<template>
  <view class="add-new">
    <view class="form-content">
      <orderForm ref="orderForm" v-if="initFinish" :form-list="formList" code-key="eqCode" :form-value="formData" :rules="rules" edit-able
                 able-scan />
    </view>
    <view class="btn-content" v-if="initFinish">
      <button :disabled="!ableSubmit" @click="submit" class="login-btn cu-btn block bg-blue lg">提交</button>
    </view>
  </view>
</template>

<script>
  import orderForm from '../../common/order-form/orderForm.vue';
  import {
    createOrder,
    getEquipmentInfo,
  } from '../../../api/device/device.js';
  
  const formList = [ {
    name: '设备名称',
    code: 'eqName',
    disable: true,
  },
    {
      name: '备注说明',
      code: 'remark',
      type: 'textarea',
    },
  ];
  
  export default {
    components: {
      orderForm,
    },
    data() {
      return {
        ableSubmit: true,
        formList,
        rules: {
          remark: {
            rules: [ {
              required: false,
              errorMessage: '请填写备注说明',
            } ],
          },
        },
        formData: {},
        initFinish: false,
      };
    },
    onShow() {
      const pageList = getCurrentPages();
      const param = pageList[ pageList.length - 1 ].options || {};
      this.formData.eqCode = param.eqCode || '';
      this.init();
    },
    methods: {
      async init() {
        await this.getDeviceInfo();
        this.initFinish = true;
      },
      async getDeviceInfo() {
        const {
          formData,
        } = this;
        if (formData.eqCode) {
          const res = await getEquipmentInfo({
            eqCode: formData.eqCode,
          });
          if (res.code === 200) {
            formData.eqName = res.data.eqName;
          }
        }
        this.formData = formData;
      },
      async submit() {
        const orderForm = this.$refs.orderForm[ 0 ] || this.$refs.orderForm;
        const {
          formData,
          isSuccess,
        } = await orderForm.getFormData();
        if (!isSuccess) {
          return;
        }
        this.ableSubmit = false;
        const res = await createOrder(formData).catch(() => {
          this.ableSubmit = true;
        });
        this.ableSubmit = true;
        if (res && res.code === 200) {
          const pageList = getCurrentPages();
          uni.showToast({
            title: '提交成功',
            icon: 'success',
          });
          if (pageList[ pageList.length - 2 ] && pageList[ pageList.length - 2 ].route === 'pages/index/index') {
            uni.navigateBack({
              delta: 1,
            });
          } else {
            uni.switchTab({
              url: '/pages/index/index',
            });
          }
          return;
        }
        uni.showToast({
          title: res.msg || '提交失败',
          icon: 'error',
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .add-new {
    
    &,
    * {
      box-sizing: border-box;
    }
    
    height: 100vh;
    background-color: white;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    
    .form-content {
      flex: 1;
      padding: 10px;
      overflow: auto;
    }
    
    .btn-content {
      padding: 10px;
    }
  }
</style>
